﻿/*
 * SharpDevelop으로 작성되었습니다.
 * 사용자: 2120
 * 날짜: 2012-09-27
 * 시간: 오전 4:07
 * 
 * 목적 : PictureBox에서 픽킹이 가능하도록 한다.
 * 이 템플리트를 변경하려면 [도구->옵션->코드 작성->표준 헤더 편집]을 이용하십시오.
 */
 
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

using PicturePicking.PickedObject;
using PicturePicking.Picking;


using RLibrary.BitmapManager;

namespace PicturePicking
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class form1 : Form
	{
		PictureManager manager = null;
		
		Bitmap bitmap = null;
		
		public form1()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void MainFormLoad(object sender, EventArgs e)
		{
			manager = PictureManager.Create();
			manager.SetPicker(PicturePicking.Picking.PicturePicker.Create(manager));
			manager.SetPictureBox(this.pictureBox1);
			
			//비트맵을 저장
			bitmap = BitmapManager.GetInstance().CreateBitmap(pictureBox1.Width,pictureBox1.Height);
			
			Graphics g = BitmapManager.GetInstance().GetDC(bitmap);
			g.DrawRectangle(BitmapManager.GetInstance().CreatePen(Color.Black),new Rectangle(10,10,50,50));
			//g.DrawString("Test String:",10,10);
			g.Dispose();
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			Random r = new Random(500);
			
			manager.Add(PickingNode.Create(new PictureNode(bitmap),10,10,40,40));
			//
			//Graphics g = BitmapManager.GetInstance().GetDC(bitmap);
			//g.DrawRectangle(BitmapManager.GetInstance().CreatePen(Color.Black),new Rectangle();
		}
		
		void Timer1Tick(object sender, EventArgs e)
		{
			BitmapManager.GetInstance().Clear(bitmap,Color.White);
			manager.Draw(null);
			BitmapManager.GetInstance().Apply(pictureBox1,bitmap);
		}
		
		void PictureBox1MouseDown(object sender, MouseEventArgs e)
		{
			
		}
		
		//오브젝트가 잡혀있다면 놓치지 않는다. 를 처리.
		IPickedable picked = null;
		void PictureBox1MouseMove(object sender, MouseEventArgs e)
		{
			if(e.Button == MouseButtons.Left){
				if(picked == null){
					picked = manager.Pick(e.X,e.Y);
				}else if(picked != null){
					form1.ActiveForm.Text = "True";
					picked.X = e.X - picked.Width/2;
					picked.Y = e.Y - picked.Height/2;
				}
			}else{
					form1.ActiveForm.Text = "False";
					picked = null;
				}
		}
	}
}

